﻿Option Explicit On
Option Strict On

Imports LogginsLogDSTableAdapters
Imports System.Data.SqlClient

Partial Class Account_EmailVerification
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load


        Dim strUserName As String = "New Associate"  'set the logged in user's name here
        Dim strNewUser As String = "Welcome " + strUserName + "<br />"

        strNewUser += "You've arrived at our site because a colleague has requested a login for you as someone who will use the site on their behalf.<br/>"
        strNewUser += "As a new user you will need to provide us with a few more basic details so that we may properly support you.<br/>"
        strNewUser += "Please click 'Continue' to continue on to the next page. <br/>"

        Dim strUnknownUser As String = "Your details are unknown." ' Unknown
        Dim strExpiredUser As String = "Sorry these details can only be used once"  ' Expired

        'Me.lblMessage.Text = strNewUser 'or one of the above

        If Not Request.QueryString("invitation") Is Nothing Then
            Dim emailstring As String = ""
            Dim loginoccurred As Boolean
            Dim validfordaysnum As Integer
            Dim personid As Integer
            Dim daterequesteddate As DateTime

            Dim invitationguid As Guid
            Try
                invitationguid = New Guid(Request.QueryString("invitation"))
            Catch ex As Exception
                'Stop an error being generated if the users tampers with the string and it is not a guid
                'Log out the user here and dump them back to the outer home page without any info 
            End Try
            Dim newpersonassociaterequestemailTA As New LogginsLogDSTableAdapters.usp_PersonAssociateRequestTableAdapter()
            Dim newpersonassociaterequestemailDT As LogginsLogDS.usp_PersonAssociateRequestDataTable

            Try
                newpersonassociaterequestemailDT = newpersonassociaterequestemailTA.GetData(invitationguid)
            Catch ex As Exception
                Throw ex
            End Try

            If Not newpersonassociaterequestemailDT.Count = 0 Then
                For Each newpersonassociaterequestemailRow As LogginsLogDS.usp_PersonAssociateRequestRow In newpersonassociaterequestemailDT
                    emailstring = newpersonassociaterequestemailRow.RequestedForEmail
                    loginoccurred = newpersonassociaterequestemailRow.LoginOccurred
                    validfordaysnum = newpersonassociaterequestemailRow.ValidForDays
                    daterequesteddate = newpersonassociaterequestemailRow.DateRequested
                    personid = newpersonassociaterequestemailRow.PersonId
                Next

                Dim days As Integer = CInt((DateTime.Now.AddDays(0 - validfordaysnum).Subtract(daterequesteddate)).TotalDays)
                'Added to check if a user's sponsor's account is active
                Dim SPTA As New LogginsLogDSTableAdapters.SPTableAdapter()
                Dim bPersonAccountIsActive As Boolean = CBool(SPTA.fn_UserAccountIsActive(personid)) 'Check if the person's account is still active

                If loginoccurred Then
                    lblInvitationMessage.Text = "Your membership is already confirmed."
                    InvitationFailModalPopupExtender.Show()
                ElseIf days > 0 Then
                    lblInvitationMessage.Text = strExpiredUser
                    InvitationFailModalPopupExtender.Show()
                ElseIf bPersonAccountIsActive Then
                    lblInvitationMessage.Text = "Sorry, the individual that invited you is no longer active. Feel free to contact un using the Contact Us page"
                    InvitationFailModalPopupExtender.Show()
                Else
                    lblMessage.Text = strNewUser
                End If
            Else
                lblInvitationMessage.Text = strUnknownUser
                InvitationFailModalPopupExtender.Show()
            End If


            'Dim newpersonassociaterequestid As Integer
            'Dim newpersonassociaterequestTA As New LogginsLogDSTableAdapters.usp_PersonAssociateRequestTableAdapter()
            'Dim newpersonassociaterequestDT As LogginsLogDS.usp_PersonAssociateRequestDataTable
            'newpersonassociaterequestDT = newpersonassociaterequestTA.GetData(invitationguid)
            'If newpersonassociaterequestDT.Rows.Count = 0 Then
            '    'Wrong request guid
            '    For Each newpersonassociaterequestRow As LogginsLogDS.usp_PersonAssociateRequestRow In newpersonassociaterequestDT
            '        newpersonassociaterequestid = newpersonassociaterequestRow.Id
            '    Next
            'End If

        End If
    End Sub
    Function AssociateValidate(ByVal userid As Integer) As Boolean

        Dim strConnString As String = ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Try
            con.Open()
            Dim com As New SqlCommand("Execute dbo.fn_UserAccountIsActive(" & userid & ")", con)
            Return (DirectCast(com.ExecuteScalar(), [Boolean]))

        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
        End Try

    End Function

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        'Response.Redirect("~/private/default.aspx") 'go to the user checks page.
        Response.Redirect("~/Account/Register.aspx?invitation=" + Request.QueryString("invitation"))

    End Sub

    Protected Sub btnCancelInvitationFail_Click(sender As Object, e As System.EventArgs) Handles btnCancelInvitationFail.Click
        HttpContext.Current.Response.Redirect("~/Default.aspx")
    End Sub
End Class
